/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.awwork.lr.checker.tools;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * 读取HTML源代码
 *
 * @author machao
 */
public class HTMLReader {

    public static String readHTML(String pageURL, String encoding) {

        StringBuilder pageHTML = new StringBuilder();

        try {
            URL url = new URL(pageURL);
            HttpURLConnection connection = (HttpURLConnection) url.openConnection();
            connection.setRequestProperty(
                    "User-Agent", "Mozilla/4.0 (compatible; MSIE 5.0; Windows NT; DigExt)");
            InputStream is = connection.getInputStream();
            InputStreamReader in = new InputStreamReader(is, encoding);
            // connection.setRequestProperty("User-Agent", "MSIE 7.0");
            BufferedReader br = new BufferedReader(in);
            String line = null;
            int i = 0;
            while ((line = br.readLine()) != null) {
                pageHTML.append(line);
                pageHTML.append("\n");
            }
            br.close();
            connection.disconnect();
        } catch (MalformedURLException ex) {
            Logger.getLogger(HTMLReader.class
                    .getName()).log(Level.SEVERE, null, ex);
        } catch (IOException ex) {
            Logger.getLogger(HTMLReader.class
                    .getName()).log(Level.SEVERE, "还没有加载完成", ex);
        }
        return pageHTML.toString();
    }

    public static String readHTML2(String pageURL, String encoding) {

        StringBuilder pageHTML = new StringBuilder();

        try {
            URL url = new URL(pageURL);
            System.out.println("a");
            HttpURLConnection connection = (HttpURLConnection) url.openConnection();
            System.out.println("b");
            connection.setRequestProperty(
                    "User-Agent", "Mozilla/4.0 (compatible; MSIE 5.0; Windows NT; DigExt)");
            InputStream is = connection.getInputStream();
            System.out.println("c");
            InputStreamReader in = new InputStreamReader(is, encoding);
            // connection.setRequestProperty("User-Agent", "MSIE 7.0");
            BufferedReader br = new BufferedReader(in);
            String line = null;
            int i = 0;
            System.out.println("d");
            while ((line = br.readLine()) != null) {
                pageHTML.append(line);
                pageHTML.append("\n");
                System.out.println(i++);
            }
            br.close();
            connection.disconnect();
        } catch (MalformedURLException ex) {
            Logger.getLogger(HTMLReader.class
                    .getName()).log(Level.SEVERE, null, ex);
        } catch (IOException ex) {
            Logger.getLogger(HTMLReader.class
                    .getName()).log(Level.SEVERE, "还没有加载完成", ex);
        }
        return pageHTML.toString();
    }

    public static String readHTML3(String pageURL, String encoding) {
        URL url;
        StringBuilder pageHTML = new StringBuilder();
        try {
            url = new URL(pageURL);
            InputStream ins = url.openStream();
            BufferedReader bReader = new BufferedReader(new InputStreamReader(ins));
            String info = bReader.readLine();
            while (info != null) {
                pageHTML.append(info);
                pageHTML.append("\n");
                info = bReader.readLine();
            }
            ins.close();
            bReader.close();
        } catch (MalformedURLException ex) {
            Logger.getLogger(HTMLReader.class
                    .getName()).log(Level.SEVERE, null, ex);
        } catch (IOException ex) {
            Logger.getLogger(HTMLReader.class
                    .getName()).log(Level.SEVERE, "还没有加载完成", ex);
        }
        return pageHTML.toString();
    }

    /**
     * 获取<a>标签全文
     *
     * @param html
     * @param subString
     * @return
     */
    public static String getAtag(String html, String subString) {
        int i = html.indexOf(subString);
        int startpoint = html.substring(0, i).lastIndexOf("<a");
        int endpoint = startpoint + 3 + subString.length() + html.substring(i).indexOf("a>") + 2;
        String s = html.substring(startpoint, endpoint);
        return s;
    }

    /**
     * 获取链接地址
     *
     * @param str
     * @return
     */
    public static String getHref(String str) {
        int i = str.indexOf("href=");
        int endpoint = str.substring(i).indexOf(" ") + i;
        if ((i + 5) > endpoint) {
            return null;
        }
        String s = str.substring(i + 5, endpoint);
        return s;

    }

    /**
     * 获取锚文本
     *
     * @param str
     * @return
     */
    public static String getText(String str) {
        int i = str.indexOf(">");
        int endpoint = str.substring(i).indexOf("<") + i;
        if ((i + 1) > endpoint) {
            return null;
        }
        String s = str.substring(i + 1, endpoint);
        return s;
    }

    /**
     * 是否包含Nofollow
     *
     * @param str
     * @return
     */
    public static String getNofollow(String str) {
        int i = str.indexOf("nofollow");
        if (i != -1) {
            return "true";
        } else {
            return "false";
        }
    }

    /**
     * 从前后n个字符范围内，查询str的数量
     *
     * @param html 全文
     * @param subString 被检查文本
     * @param n
     * @param str
     * @return 数量
     */
    public static int getAtagFromText(String html, String subString, int n, String str) {
        int i = html.indexOf(subString);
        String s = html.substring(i - n, i + n);
        Pattern ptn = Pattern.compile(str);
        Matcher mat = ptn.matcher(s);
        int count = 0;
        while (mat.find()) {
            count++;
        }
        return count - 1;
    }

    public static void main(String args[]) {
        System.out.println(1);
        String html = readHTML3("http://www.citybike-prague.com/our-friends/", "UTF-8");
        String subString = "chine-evasion.com";
        html = html.replace("\t", " ");
        //String a = getAtag(html, subString);
        //String s = getNofollow("<a href=\"http://www.chine-evasion.com\" target=\"_blank\" title=\"Voyage en Chine\">Voyages en individuel et sur mesure en Chine, Chine Evasion Voyages</a>");
        //getHref(a);
        int a = getAtagFromText(html, subString, 1500, "<a ");
        System.out.println(a);
    }
}